function reply(name, id) {
	$("#toMemberId").val(id);
	var content = $(editor.codemirror.display.input);
	var oldContent = editor.value();
	var prefix = "回复 " + name + " ：";
	var newContent = '';
	console.log(oldContent, prefix);
	if (oldContent.length > 0) {
		console.log(oldContent == prefix);
		if (oldContent != prefix) {
			console.log(1);
			newContent = oldContent + '\n' + prefix;
		}
	} else {
		console.log(2);
		newContent = prefix
	}
	editor.value(newContent);
	CodeMirror.commands.goDocEnd(editor.codemirror);
	content.focus();
	moveEnd(content);
}

var moveEnd = function(obj) {
	obj.focus();
	obj = obj.get(0);
	var len = obj.value.length;
	if (document.selection) {
		var sel = obj.createTextRange();
		sel.moveStart('character', len);
		sel.collapse();
		sel.select();
	} else if (typeof obj.selectionStart == 'number'
			&& typeof obj.selectionEnd == 'number') {
		obj.selectionStart = obj.selectionEnd = len;
	}
};

function replySubmit() {
	var errorMsg = $("#error_message");
	var content = editor.value();
	var topicId = $("#topicId").val();
	var toMemberId = $("#toMemberId").val();
	var defaultToMemberId = $("#defaultToMemberId").val();
	if (content.length == 0) {
		errorMsg.html("回复内容不能为空");
		return;
	}
	
	$.ajax({
		type : 'POST',
		url : ctx + '/topic/addReply.do',
		data : {
			content : content,
			toMemberId : toMemberId,
			defaultToMemberId : defaultToMemberId,
			topicId : topicId
		},
		dataType : 'json',
		timeout : 30000,
		context : $('body'),
		success : function(data) {
			if (data.code == "ok") {
				location.reload();
			} else {
				errorMsg.html(data.msg);
			}
		},
		error : function(xhr, type) {
			errorMsg.html('提交失败!');
		}
	});
}

$(function() {
	var n = $("#goTop");
	n.click(function() {
		return $("html,body").animate({
			scrollTop : 0
		});
	});
});

function changeCollect(){
	var isCollect = $("#isCollect").val();
	var topicId = $("#topicId").val();
	var collect = $("#collect");
	$.ajax({
		type : 'POST',
		url : ctx + '/member/changCollectionStatus.do',
		data : {
			isCollection : isCollect,
			topicId : topicId,
			type : 0
		},
		dataType : 'json',
		timeout : 30000,
		context : $('body'),
		success : function(data) {
			if (data.code == "ok") {
				if(data.object == true){
					collect.html('<span class='+'"glyphicon glyphicon-star"'+'></span>取消收藏');
					$("#isCollect").val(true);
				}else{
					collect.html('<span class='+'"glyphicon glyphicon-star-empty"'+'></span>加入收藏');
					$("#isCollect").val(false);
				}
			} else {
				showTip2(data.msg);
			}
		},
		error : function(xhr, type) {
			showTip2('提交失败!');
		}
	});
}

function replyPage(pageNo, pageSize){
	var topicId = $("#topicId").val();
	var replyListDiv = $("#replyList");
	$.ajax({
		type : 'GET',
		url : ctx + '/topic/findReplyPage.do',
		data : {
			pageNo : pageNo,
			pageSize : pageSize,
			topicId : topicId
		},
		dataType : 'json',
		timeout : 30000,
		context : $('body'),
		success : function(data) {
			if (data.code == "ok") {
				
//				var replyData = data.page;
//				var list = replyData.list;
//				alert(list.size());
				for (obj in data.page.list) {
					alert(111);
					obj = data.page.list[obj];
					var html = '<div class="media" id="reply">'
						+'<div class="media-left">'
							+'<a href="#"><img src="${ctx}/page/include/avatar.png" class="avatar" alt="" /></a>'
						+'</div>'
						+'<div class="media-body reply-content">'
							+'<div class="media-heading gray">'
								+'<a href="#">'+obj.member.name+'</a>'
									+111
								+'<span class="pull-right">'
								+'<a href="#">编辑</a>'
								+'<a href="#">删除</a>'
								+'<a href="javascript:reply('+obj.member.name+');">回复</a>'
								+'</span>'
							+'</div>'
							+'<p>'+111111+'</p>'
							+'</div>'
							+'</div>'
							+'<div class="divide"></div>';
				}
				replyListDiv.append(html);
			} else {
				showTip2(data.msg);
			}
		},
		error : function(xhr, type) {
			showTip2('提交失败!');
		}
	});
}
